package com.cction.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.cction.R
import com.cction.adapter.FragmentStateAdapter
import com.cction.entry.TabEntity
import com.cction.fragment.CctionFragment
import com.cction.fragment.SimulationFragment
import com.cction.fragment.UserFragment
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.main.*
import java.util.ArrayList

/**
 * 主页
 */
class MainTabActivity : AppCompatActivity() {
    private val context = this

    //        private val mTitles = arrayOf("计算", "模拟", "我的")
//    private val mIconUnselectIds = intArrayOf(R.drawable.svg_cction_unselect, R.drawable.svg_moni_unselect, R.drawable.svg_user_unselect)
//    private val mIconSelectIds = intArrayOf(R.drawable.svg_cction_select, R.drawable.svg_moni_select, R.drawable.svg_user_select)
    private val mTitles = arrayOf("计算", "我的")
    private val mIconUnselectIds = intArrayOf(R.drawable.svg_cction_unselect, R.drawable.svg_user_unselect)
    private val mIconSelectIds = intArrayOf(R.drawable.svg_cction_select, R.drawable.svg_user_select)

    private val mFragments = ArrayList<Fragment>()
    private val mTabEntities = ArrayList<CustomTabEntity>()

    private lateinit var viewpagerAdapter: FragmentStatePagerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)


        initView()
    }


    private fun initView() {

        mFragments.add(CctionFragment())
//        mFragments.add(SimulationFragment())
        mFragments.add(UserFragment())

        (mTitles.indices).forEach {
            mTabEntities.add(TabEntity(mTitles[it], mIconSelectIds[it], mIconUnselectIds[it]))
        }

        id_tablayout.setTabData(mTabEntities)

        viewpagerAdapter = FragmentStateAdapter(supportFragmentManager, mFragments)
        id_viewpager.adapter = viewpagerAdapter


        id_viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(index: Int) {

            }

            override fun onPageScrolled(index: Int, p1: Float, p2: Int) {

            }

            override fun onPageSelected(index: Int) {
                id_tablayout.currentTab = index
            }
        })

        id_tablayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                id_viewpager.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })
    }

    public override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    public override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }

}
